package com.gmadmin.common;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.gmadmin.utils.SecurityUtils;
import org.apache.ibatis.reflection.MetaObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * @author zhangliang
 * @version 1.0
 * @date 2021/1/20 13:14
 * mybatis-plus默认通用字段填充
 */
@Component
public class CommonFieldHandler implements MetaObjectHandler {

    protected static final Logger logger = LoggerFactory.getLogger(CommonFieldHandler.class);

    @Override
    public void insertFill(MetaObject metaObject) {
        logger.info("start insert fill ...");
        //需要填充字段，名称、类型和实体类一致
        this.setFieldValByName("createTime", LocalDateTime.now(), metaObject);
        this.setFieldValByName("createBy", SecurityUtils.getCurrentUser().getUsername(),metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        logger.info("start update fill ....");
        this.setFieldValByName("updateTime", LocalDateTime.now(), metaObject);
        this.setFieldValByName("pwdResetTime", LocalDateTime.now(), metaObject);
        this.setFieldValByName("updateBy",SecurityUtils.getCurrentUser().getUsername(),metaObject);


    }
}
